/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.dao.impl;

import cc.altius.dao.PartnerDao;
import cc.altius.model.*;
import java.util.List;
import javax.sql.DataSource;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author jitendra
 */
@Repository
public class PartnerDaoImpl implements PartnerDao {

    @Autowired
    private SessionFactory sessionFactory;
    private Session session;
    private Query query;

    @Override
    @Transactional
    public List<PartnerDetail> getPartnerDetailList() {
        return this.sessionFactory.getCurrentSession().getNamedQuery("PartnerDetail.findAll").list();
    }

    @Override
    @Transactional
    public List<PartnerType> getPartnerTypeList() {
        return this.sessionFactory.getCurrentSession().getNamedQuery("PartnerType.findAll").list();
    }

    @Override
    @Transactional
    public int addPartnerDetail(PartnerDetail partnerDetail) {
        return (Integer) this.sessionFactory.getCurrentSession().save(partnerDetail);
    }

    @Override
    @Transactional
    public int addPartner(Partner partner) {
        return (Integer) this.sessionFactory.getCurrentSession().save(partner);
    }

    @Override
    @Transactional
    public List<Partner> getPartnerList(int centerId, int partnerTypeId) {

            session = this.sessionFactory.getCurrentSession();
        String hqlQuery = "from Partner p  WHERE 1=1 ";
            if (centerId != 0) {
            hqlQuery += " AND p.HealthSpringCenter.centerId=" + centerId + "";
            }
            if (partnerTypeId != 0) {
            hqlQuery += " AND p.partnerType.partnerTypeId=" + partnerTypeId + "";
            }
        hqlQuery += " ORDER BY p.priority,p.partnerDetail.name";
        
        try{
            session = this.sessionFactory.getCurrentSession(); 
            return session.createQuery(hqlQuery).list();
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
        
//        try {
//            session = this.sessionFactory.getCurrentSession();
//            String hqlQuery = " select pd1.partnerDetailId from Partner p left join p.partnerDetail  pd1 left join p.HealthSpringCenter hc left join p.partnerType pt WHERE 1=1 ";
//            System.out.println("hqlQuery :"+hqlQuery);
//            if (centerId != 0) {
//                hqlQuery += " AND hc.centerId=" + centerId + "";
//            }
//            if (partnerTypeId != 0) {
//                hqlQuery += " AND pt.partnerTypeId=" + partnerTypeId + "";
//            }
//            query = session.createQuery(" From PartnerDetail pd where pd.partnerDetailId in (" + hqlQuery + " )");
//            
//            System.out.println("size in dao impl :" + query.list().size());
//            return query.list();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
    }

    @Override
    @Transactional
    public Partner getPartnerById(int partnerId) {
        Session session = this.sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(Partner.class);
        criteria.add(Restrictions.eq("partnerID", partnerId));
        return (Partner) criteria.uniqueResult();
    }

    @Override
    @Transactional
    public int updatePartner(Partner partner) {
        Session session = this.sessionFactory.getCurrentSession();
//        System.out.println("partner :"+partner);
//        System.out.println("partner.getHealthSpringCenter().getCenterId() :"+partner.getHealthSpringCenter().getCenterId());
//        System.out.println("partner.getPartnerDetail().getPartnerDetailId() :"+partner.getPartnerDetail().getPartnerDetailId());
//        System.out.println("partner.getPartnerType().getPartnerTypeId() :"+partner.getPartnerType().getPartnerTypeId());
//        System.out.println("partner.getPriority():"+partner.getPriority());
//        System.out.println("partner.getPartnerID() :"+partner.getPartnerID());
//        
        String hql = "Update "
                + " Partner as p"
                + " set"
                + " p.HealthSpringCenter.centerId=:centerId,"
                + " p.partnerDetail.partnerDetailId=:partnerDetailId,"
                + " p.partnerType.partnerTypeId=:partnerTypeId,"
                + " p.priority=:priority"
                + " WHERE p.partnerID=:partnerID";

        Query query = session.createQuery(hql);
        query.setParameter("centerId", partner.getHealthSpringCenter().getCenterId());
        query.setParameter("partnerDetailId", partner.getPartnerDetail().getPartnerDetailId());
        query.setParameter("partnerTypeId", partner.getPartnerType().getPartnerTypeId());
        query.setParameter("priority", partner.getPriority());
        query.setParameter("partnerID", partner.getPartnerID());

        int result = query.executeUpdate();
        return result;
    }

    @Override
    @Transactional
    public PartnerDetail getPartnerDetailById(int partnerDetailId) {
        Session session = this.sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(PartnerDetail.class);
        criteria.add(Restrictions.eq("partnerDetailId", partnerDetailId));
        return (PartnerDetail) criteria.uniqueResult();
    }

    @Override
    @Transactional
    public int updatePartnerDetail(PartnerDetail partnerDetail) {
        Session session = this.sessionFactory.getCurrentSession();
        String hql = "Update "
                + " PartnerDetail as p"
                + " set"
                + " p.name=:name,"
                + " p.address=:address,"
                + " p.phone=:phone,"
                + " p.contact1Person=:contact1Person,"
                + " p.contact1Number=:contact1Number,"
                + " p.contact2Person=:contact2Person,"
                + " p.contact2Number=:contact2Number,"
                + " p.contact3Person=:contact3Person,"
                + " p.contact3Number=:contact3Number,"
                + " p.description=:description"
                + " WHERE p.partnerDetailId=:partnerDetailId";

        Query query = session.createQuery(hql);
        query.setParameter("name", partnerDetail.getName());
        query.setParameter("address", partnerDetail.getAddress());
        query.setParameter("phone", partnerDetail.getPhone());
        query.setParameter("contact1Person", partnerDetail.getContact1Person());
        query.setParameter("contact1Number", partnerDetail.getContact1Number());
        query.setParameter("contact2Person", partnerDetail.getContact2Person());
        query.setParameter("contact2Number", partnerDetail.getContact2Number());
        query.setParameter("contact3Person", partnerDetail.getContact3Person());
        query.setParameter("contact3Number", partnerDetail.getContact3Number());
        query.setParameter("description", partnerDetail.getDescription());
        query.setParameter("partnerDetailId", partnerDetail.getPartnerDetailId());

        int result = query.executeUpdate();
        return result;
    }
}
